<?php
require_once('../../config.php');
Doctrine::loadModels('../../models');

if( $_REQUEST["agent_id"])
{
	
	$agent_id = strip_tags($_REQUEST["agent_id"]);
    $name_short = strip_tags($_REQUEST["name_short"]);
    $name_full = strip_tags($_REQUEST["name_full"]);
	$agent_type_id =  strip_tags($_REQUEST["agent_type_id"]);
    $date_start = date("Y-m-d H:m:s", strtotime(strip_tags($_REQUEST["date_start"])));
    $date_end = date("Y-m-d H:m:s", strtotime(strip_tags($_REQUEST["date_end"])));
    $modified = date ("Y-m-d H:m:s");    

    $q = Doctrine_Query::create();

	$rows = $q->update('Agent')
          		->set('name_short', addslashes($name_short) , 
          			'name_full', addslashes($name_full),
          			'date_start', $date_start,
          			'date_end', $date_end,
          			'modified', $modified)
          ->where("agent_id = '".$agent_id."'")
          ->execute();	    		
          
    // AgentType 17, 29 do not require extension to AGENT base table
	if(($agent_type_id == 12)) // People
 	{	    
    	$title_id = strip_tags($_REQUEST["title_id"]);
		$gender_id = strip_tags($_REQUEST["gender_id"]);
		$primary_role_id = strip_tags($_REQUEST["primary_role_id"]);
		$email = strip_tags($_REQUEST["email"]);
		$telephone = strip_tags($_REQUEST["telephone"]);
		$mobile = strip_tags($_REQUEST["mobile"]);
		$im_name = strip_tags($_REQUEST["im_name"]);
		$im_service_id = strip_tags($_REQUEST["im_service_id"]);
		$fax = strip_tags($_REQUEST["fax"]);
		$website = strip_tags($_REQUEST["website"]);
		
    $qp = Doctrine_Query::create();

	$rows = $qp->update('People')
          ->set('title_id', '"'.$title_id.'",',
          		'gender_id', '"'.$gender_id.'",',
          		'primary_role_id', '"'.$primary_role_id.'",',
          		'email', '"'.$date_end.'",',
          		'telephone', '"'.$telephone.'",',
          		'mobile', '"'.$mobile.'",',
          		'im_name', '"'.$im_name.'",',
          		'im_service_id', '"'.$im_service_id.'",',
          		'fax', '"'.$fax.'",',
          		'website', '"'.$website.'"' )
          ->where('agent_person_id ='.$agent_id)
          ->execute();	    		

 	}
 	elseif (($agent_type_id == 15))  // Country
 	{
	    $membership_type_id = strip_tags($_REQUEST["membership_type_id"]);
		$country_iso_code_3 = strip_tags($_REQUEST["country_iso_code_3"]);
		$country_iso_code_2 = strip_tags($_REQUEST["country_iso_code_2"]);
		$status_id = strip_tags($_REQUEST["status_id"]);
		$continent_id = strip_tags($_REQUEST["continent_id"]);
		$region_id = strip_tags($_REQUEST["region_id"]);
		$subregion_id = strip_tags($_REQUEST["subregion_id"]);
		$development_rating_id = strip_tags($_REQUEST["development_rating_id"]);
		$sign_mou_2001 = date("Y-m-d H:m:s", strtotime(strip_tags($_REQUEST["sign_mou_2001"])));
		$sign_mou_2007 = date("Y-m-d H:m:s", strtotime(strip_tags($_REQUEST["sign_mou_2007"])));
		$mou_signed_by_id = strip_tags($_REQUEST["mou_signed_by_id"]);
	 
		$qc = Doctrine_Query::create();
		
		$rows = $qc->update('CountriesParticipants')
          ->set('membership_type_id', '"'.$membership_type_id.'",',
          		'country_iso_code_3', '"'.$country_iso_code_3.'",',
          		'country_iso_code_2', '"'.$country_iso_code_2.'",',
          		'status_id', '"'.$status_id.'",',
          		'continent_id', '"'.$continent_id.'",',
          		'region_id', '"'.$region_id.'",',
          		'subregion_id', '"'.$subregion_id.'",',
          		'development_rating_id', '"'.$development_rating_id.'",',
          		'sign_mou_2001', '"'.$sign_mou_2001.'",',
          		'sign_mou_2007', '"'.$sign_mou_2007.'",',
          		'mou_signed_by_id', '"'.$mou_signed_by_id.'"' )
          ->where('agent_country_id ='.$agent_id)
          ->execute();	    	 	
 	
 	}
 	elseif (($agent_type_id == 16))
 	{
 		// Events
	    $event_type_id = strip_tags($_REQUEST["event_type_id"]);
		$event_status_id = strip_tags($_REQUEST["event_status_id"]);
		$language_id = strip_tags($_REQUEST["language_id"]);
		$event_audience_id = strip_tags($_REQUEST["event_audience_id"]);

		$qe = Doctrine_Query::create();
		
		$rows = $qe->update('GbifEvents')
          ->set('event_type_id', '"'.$event_type_id.'",',
          		'event_status_id', '"'.$event_status_id.'",',
          		'language_id', '"'.$language_id.'",',
          		'event_audience_id', '"'.$event_audience_id.'"' )
          ->where('agent_event_id ='.$agent_id)
          ->execute();	    	 			

 	}
 	elseif (($agent_type_id == 13))
 	{
 		//Projects
	    $type_id = strip_tags($_REQUEST["type_id"]);
		$status_id = strip_tags($_REQUEST["status_id"]);
		$reference_no = strip_tags($_REQUEST["reference_no"]);
		$date_application = date("Y-m-d H:m:s", strtotime(strip_tags($_REQUEST["date_application"])));
		$date_awarded = date("Y-m-d H:m:s", strtotime(strip_tags($_REQUEST["date_awarded"])));
		$budget_awarded = strip_tags($_REQUEST["budget_awarded"]);
		$budget_released = strip_tags($_REQUEST["budget_released"]);
		
		$qpr = Doctrine_Query::create();
		
		$rows = $qpr->update('Projects')
          ->set('type_id', '"'.$type_id.'",',
          		'status_id', '"'.$status_id.'",',
          		'reference_no', '"'.$reference_no.'",',
          		'date_application', '"'.$date_application.'",',
          		'date_awarded', '"'.$date_awarded.'",',
          		'budget_awarded', '"'.$budget_awarded.'",',
          		'budget_released', '"'.$budget_released.'"')
          ->where('agent_project_id ='.$agent_id)
          ->execute();	    	 	
		 		
 	}
 	elseif (($agent_type_id == 14)) // Nodes
 	{
 	    $node_type_id = strip_tags($_REQUEST["node_type_id"]);
		$dev_status_id = strip_tags($_REQUEST["dev_status_id"]);
		$data_mob_id = strip_tags($_REQUEST["data_mob_id"]);
		
		$qn = Doctrine_Query::create();
		
		$rows = $qn->update('Nodes')
          ->set('node_type_id', "'".$node_type_id."'",
          		'dev_status_id', "'".$dev_status_id."'",
          		'data_mob_id', "'".$data_mob_id."'" )
          ->where('agent_node_id ='.$agent_id)
          ->execute();	    	 	

 	}
 	elseif (($agent_type_id == 18))
 	{
 		//Organisations
 	 	    $purpose_id = strip_tags($_REQUEST["purpose_id"]);
			$gbif_status_id = strip_tags($_REQUEST["gbif_status_id"]);
			$phone = strip_tags($_REQUEST["phone"]);
			$fax = strip_tags($_REQUEST["fax"]);
			$email = strip_tags($_REQUEST["email"]);
			$website = strip_tags($_REQUEST["website"]);
		
		$qo = Doctrine_Query::create();

		$rows = $qo->update('Organisations')
          ->set('purpose_id', '"'.$purpose_id.'",',
          		'gbif_status_id', '"'.$gbif_status_id.'",',
          		'phone', '"'.$phone.'",',
          		'fax', '"'.$fax.'",',
          		'email', '"'.$email.'",',
          		'website', '"'.$website.'"' )
          ->where('agent_org_id ='.$agent_id)
          ->execute();	    	 	
 	
 	} 	
 	elseif (($agent_type_id == 19))
 	{
 		//Suppliers
 	 	    $line_of_business_id = strip_tags($_REQUEST["line_of_business_id"]);
 	 	    $cvr_number = strip_tags($_REQUEST["cvr_number"]);
			$phone = strip_tags($_REQUEST["phone"]);
			$fax = strip_tags($_REQUEST["fax"]);
			$email = strip_tags($_REQUEST["email"]);
			$website = strip_tags($_REQUEST["website"]);
		
		$qs = Doctrine_Query::create();
			
		$rows = $qs->update('Suppliers')
          ->set('line_of_business_id', '"'.$line_of_business_id.'",',
          		'cvr_number', '"'.$cvr_number.'",',
          		'phone', '"'.$phone.'",',
          		'fax', '"'.$fax.'",',
          		'email', '"'.$email.'",',
          		'website', '"'.$website.'"' )
          ->where('agent_supplier_id ='.$agent_id)
          ->execute();	    	 	
 	} 	
 	
 	//mysql_free_result( $resultSpecificAgent );
    
 	$dom = new DomDocument('1.0');
	$dom->formatOutput = true;

	$root = $dom->createElement( "results" );
		$dom->appendChild( $root );
			$dn = $dom->createElement( "record" );
			$dn->setAttribute( 'updated_agent_id', $agent_id );
		$root->appendChild( $dn );	

	header( "Content-type: text/xml");
	echo $dom->saveXML();

}  
?>